#include "file_crypto.h"
#include "sm3.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

int main(int argc, char *argv[])
{
	int nret;
	char password[129] = {0};
	int password_len;
	char key[16 + 1] = {0}; 
	char plainFileName[256] = {0};
	char cipherFileName [256] = {0};

	if(argc != 4)
	{
		printf("错误：参数不足。\n用法：./fileDec 口令(至少6位) 密文文件名绝对路径 明文文件名绝对路径。\n例如：./fileDec 12345678 /opt/cipher.txt /opt/plain.txt\n");
		return -1;
	}

	password_len = strlen(argv[1]);
	if((password_len < 6) || (password_len > 128))
	{
		printf("口令长度至少6位，最大128位\n");
		return -1;
	}

	if((strlen(argv[2]) > 256) || (strlen(argv[3]) > 256))
	{
		printf("文件名长度不能超过256位\n");
		return -1;
	}

	strcpy(password, argv[1]);
	strcpy(cipherFileName, argv[2]);
	strcpy(plainFileName, argv[3]);
	
	//派生密钥
	sm3((unsigned char *)password, password_len, (unsigned char *)key);

	//解密文件
	nret = symFileDec(key, cipherFileName, plainFileName);
	if(nret != R_OK)
	{
		printf("symFileDec failed, nret: %d\n", nret);
		return -1;
	}

	printf("file decrypt success...\n");

	return 0;
}

